/**
 * @file: IHM.java
 * @author: Bon William <william.bon2@gmail.com>
 * @author: Ernewein Audrey <audrey.ernewein@gmail.com>
 * @brief:
 * This file contains functions to build the IHM and adding actions to be executed
 * when pressing the buttons.
 */
package Parser;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class IHM extends JFrame {
	JButton generateButton = new JButton("Générer les stats");
	JButton generateAll = new JButton("Générer les données");
	JButton setWorkingDir = new JButton("Choisir un répertoire");
	
	JPanel workingDirPanel = new JPanel();
	JPanel buttonPanel = new JPanel();
	JPanel argPanel = new JPanel();
	JPanel combo1Panel= new JPanel();
	JPanel combo2Panel = new JPanel();
	
	GridLayout grid = new GridLayout(2,1);
	
	JLabel lblFile = new JLabel("Choisir un fichier :");
	JLabel lblVariante = new JLabel("Choisir une variante :");
	JLabel workingDirLabel = new JLabel(" ");
	
	String workingDir =new String();
	String[] fileStrings = { "eukarya", "archaea", "bacteria" };
	JComboBox fileList = new JComboBox(fileStrings);
	
	String[] varianteStrings = { "base", "décalé de 1", "décalé de 2", "code X" };
	JComboBox varianteList = new JComboBox(varianteStrings);
	
	
	/**
	 * IHM main constructor:
	 * builds the IHM elements and position it inside the window
	 */
	public IHM() {
			
		/* Adding action listener to buttons */
		setWorkingDir.addActionListener(new boutonWorkingDirListener());
		generateButton.addActionListener(new boutonGenerateListener());
		generateAll.addActionListener(new boutonGenerateListener());
		
		/* Panel creation and positioning */
		workingDirPanel.add(setWorkingDir,BorderLayout.SOUTH);
		workingDirPanel.add(workingDirLabel,BorderLayout.NORTH);
		
		combo1Panel.add(lblFile);
		combo1Panel.add(fileList);
		combo2Panel.add(lblVariante);
		combo2Panel.add(varianteList);
		
		argPanel.setLayout(grid);
		argPanel.add(combo1Panel);
		argPanel.add(combo2Panel);

		buttonPanel.add(generateButton,BorderLayout.EAST);
		
		buttonPanel.add(generateAll,BorderLayout.WEST);

		/* setting window options (name, size, and contents */
		this.setVisible(true);
		this.setTitle("BioInformatique");
		this.setSize(400, 200);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.add(argPanel,BorderLayout.NORTH);
		this.add(workingDirPanel,BorderLayout.CENTER);
		this.add(buttonPanel,BorderLayout.SOUTH);
	}
	
	/* Inside class for working directory button listener */
	class boutonWorkingDirListener implements ActionListener
	{
		/**
		 * actionPerformed
		 * Callback called when pressing on "Set working directory" button
		 */
		public void actionPerformed(ActionEvent arg0)
		{
	        
		    JFileChooser chooser = new JFileChooser(); 
		    chooser.setCurrentDirectory(new java.io.File("."));
		    chooser.setDialogTitle("Select working directory");
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    //
		    // disable the "All files" option.
		    //
		    chooser.setAcceptAllFileFilterUsed(false);
		    
		    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
		      workingDir=chooser.getSelectedFile().toString()+"/";
		      workingDirLabel.setText(workingDir);
		      }
		}
	}
	
	/* Inside class for buttons' listener */
	class boutonGenerateListener implements ActionListener
	{
		/**
		 * actionPerformed
		 * Callback called when pressing on a button
		 */
		public void actionPerformed(ActionEvent arg0)
		{	
			if(workingDir.isEmpty())
			{
				System.out.println(" ");
				JOptionPane.showMessageDialog((Component) arg0.getSource(), "Aucun répertoire n'a été choisi pour exporter les fichiers !");
				return ;
			}
			String fileName = (String)fileList.getSelectedItem();
			/* User press "generate" button */
			if(arg0.getSource().equals(generateButton))
			{
				Stats stat = new Stats();
				stat.getAllTrinucleotides(workingDir,fileName, varianteList.getSelectedIndex());
				try {
					stat.writeFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
			else // User press "generate all" button
			{
				try {
					new BioParser().extractLinkByType(workingDir,fileName);
				} catch (IOException e) {
					e.printStackTrace();
				}
				System.out.println("Votre fichier a été généré.\n");
			}
		}
	
	}

}

